package com.foreknow.elm.controller;

import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.impl.DeliveryAddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class DeliveryAddressController {
    /**
     * 根据用户编号查询所属送货地址
     * @return
     * @throws Exception
     */
    public Object listDeliveryAddressByUserId(HttpServletRequest request)throws Exception{
        String userId = request.getParameter("userId");
        DeliveryAddressServiceImpl deliveryAddressService = new DeliveryAddressServiceImpl();
        List<Deliveryaddress> list = deliveryAddressService.listDeliveryAddressByUserId(userId);
        return list;
    }

    /**
     * 根据送货地址编号查询送货地址
     * @param
     * @return deliveryaddress
     */
    public Deliveryaddress getDeliveryAddressById(HttpServletRequest request){
        Deliveryaddress deliveryaddress = null;
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        DeliveryAddressServiceImpl deliveryAddressService = new DeliveryAddressServiceImpl();
        try {
            deliveryaddress = deliveryAddressService.getDeliveryAddressById(daId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deliveryaddress;
    }

    /**
     * 向送货地址表中添加一条记录
     * @param request
     * @return
     */
    public int saveDeliveryAddress(HttpServletRequest request){
        int result = 0;
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        String contactName = request.getParameter("contactName");
        Integer contactSex = Integer.valueOf(request.getParameter("contactSex"));
        String contactTel = request.getParameter("contactTel");
        String address = request.getParameter("address");
        String userId = request.getParameter("userId");
        Deliveryaddress deliveryaddress = new Deliveryaddress(daId,contactName,contactSex,contactTel,address,userId);
        DeliveryAddressServiceImpl deliveryAddressService = new DeliveryAddressServiceImpl();
        try {
            result = deliveryAddressService.saveDeliveryAddress(deliveryaddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据送货地址编号更新送货地址信息
     * @param request
     * @return
     */
    public int updateDeliveryAddress(HttpServletRequest request){
        int result = 0;
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        String contactName = request.getParameter("contactName");
        Integer contactSex = Integer.valueOf(request.getParameter("contactSex"));
        String contactTel = request.getParameter("contactTel");
        String address = request.getParameter("address");
        String userId = request.getParameter("userId");
        Deliveryaddress deliveryaddress = new Deliveryaddress(daId,contactName,contactSex,contactTel,address,userId);
        DeliveryAddressServiceImpl deliveryAddressService = new DeliveryAddressServiceImpl();
        try {
            result = deliveryAddressService.updateDeliveryAddress(deliveryaddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据送货地址编号删除一条记录
     * @param request
     * @return
     */
    public int removeDeliveryAddress(HttpServletRequest request){
        int result = 0;
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        DeliveryAddressServiceImpl deliveryAddressService = new DeliveryAddressServiceImpl();
        try {
            result = deliveryAddressService.removeDeliveryAddress(daId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
